<?php

namespace App\V3\V3_2_7\Controllers;

use App\Http\Requests\V3_2_7\AbsenceRequest;
use App\Http\Requests\V3_2_7\AgreeRefuseRequest;
use App\Http\Requests\V3_2_7\EmployeeStatusRequest;
use App\Http\Requests\V3_2_7\IsRepealRequest;
use App\Http\Requests\V3_2_7\PageRequest;
use App\Http\Requests\V3_2_7\SetTimeRequest;
use App\V3\V3_2_7\Services\EmployeeStatusService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;
use Carbon\Carbon;

class EmployeeStatusController extends BaseController
{
    /**
     *主账号设置/取消时间
     * Author: zlf
     * Created at 2024/8/6 10:03
     * @param SetTimeRequest $request
     * @return JsonResponse
     */
    public function setTime(SetTimeRequest $request): JsonResponse
    {
        if ($this->getUserTpye() !== 0) {
            return $this->fail('', '请使用主账号设置！');
        }
        $data = $request->all();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        $this->operateLog('主账号设置时间', $data, time());
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data) {
                // 锁定 10 秒并自动释放...
                return EmployeeStatusService::setTime($data);
            });
    }

    /**
     *主账号设置时间列表
     * Author: zlf
     * Created at 2024/8/6 11:28
     * @param Request $request
     * @return JsonResponse
     */
    public function getTimeList(Request $request): JsonResponse
    {
        $data = $request->all();
        $data['main_id'] = $this->getMainUserId();
        if (!empty($data['set_time'])) {
            $date = Carbon::createFromFormat('Y-m-d', $data['set_time']);
            $data['start_time'] = $date->startOfMonth()->toDateString();
            $data['end_time'] = $date->endOfMonth()->toDateString();
        } else {
            $data['start_time'] = Carbon::today()->startOfMonth()->toDateString();
            $data['end_time'] = Carbon::today()->endOfMonth()->toDateString();
        }
        return $this->success(EmployeeStatusService::getTimeList($data));
    }

    /**
     *员工添加休息申请
     * Author: zlf
     * Created at 2024/8/6 15:09
     * @param AbsenceRequest $request
     * @return JsonResponse
     */
    public function addAbsenceRequest(AbsenceRequest $request): JsonResponse
    {
        $data = $request->all();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        $this->operateLog('员工添加休息申请', $data, time());
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data) {
                // 锁定 10 秒并自动释放...
                return EmployeeStatusService::addAbsenceRequest($data);
            });
    }

    /**
     *休息与工作人员列表
     * Author: zlf
     * Created at 2024/8/6 17:05
     * @param EmployeeStatusRequest $request
     * @return JsonResponse
     */
    public function restWorkList(EmployeeStatusRequest $request): JsonResponse
    {
        $data = $request->all();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        if (empty($data['set_time'])) {
            $data['set_time'] = Carbon::today()->toDateString();
        }
        return $this->success(EmployeeStatusService::restWorkList($data));
    }

    /**
     *休息与工作人员列表
     * Author: zlf
     * Created at 2024/8/6 17:05
     * @param PageRequest $request
     * @return JsonResponse
     */
    public function seeMoreList(PageRequest $request): JsonResponse
    {
        $data = $request->all();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        return $this->success(EmployeeStatusService::seeMoreList($data));
    }

    /**
     *员式工作状态列表
     * Author: zlf
     * Created at 2024/8/6 17:05
     * @param Request $request
     * @return JsonResponse
     */
    public function employeeStatusList(Request $request): JsonResponse
    {
        $data = $request->all();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        return $this->success(EmployeeStatusService::employeeStatusList($data));
    }

    public function newRestInfo(Request $request): JsonResponse
    {
        $data = $request->all();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        return $this->success(EmployeeStatusService::newRestInfo($data));
    }

    /**
     *主账号同意或拒绝
     * Author: zlf
     * Created at 2024/8/8 9:45
     * @param AgreeRefuseRequest $request
     * @return JsonResponse
     */
    public function agreeOrRefuse(AgreeRefuseRequest $request): JsonResponse
    {
        if ($this->getUserTpye() !== 0) {
            return $this->fail('', '请使用主账号审核！');
        }
        $data = $request->all();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        $this->operateLog('员工状态审核', $data, time());
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data) {
                // 锁定 10 秒并自动释放...
                return EmployeeStatusService::agreeRefuseRepeal($data, 1);
            });
    }

    /**
     *撤销
     * Author: zlf
     * Created at 2024/8/8 9:25
     * @param IsRepealRequest $request
     * @return mixed
     */
    public function isRepeal(IsRepealRequest $request)
    {
        $data = $request->all();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        $this->operateLog('员工状态撤销', $data, time());
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data) {
                // 锁定 10 秒并自动释放...
                return EmployeeStatusService::agreeRefuseRepeal($data,0);
            });
    }

}
